/**
 * 保定市政府采购网爬虫
 */
import {Base,Item} from '../base';
class bdcg extends Base{
  constructor(){
      super();
      this.name='保定市政府采购网';
      this.enabled = true;
      this.encode = "utf-8";
      this.debug = false;
      this.timeout = 1000000;
      this.collection = "ztb";
      this.collectionShowName="招投标信息";
      this.keyField = "title";
  }
  getItems(pageIndex:number) :Item[] {
      let num =pageIndex+1;
      const url = `http://www.bd.gov.cn/index.do?view=rsrmlist&ccid=382&page=${num}`;
      let pageCount =this.getPageCount();
      let content =this.spider.getSync(url);
      let $ =this.cheerio.load(content);
      let result  :Item[] =[];
      let that =this;
        try {
            $('#r_main1 table').eq(1).find('tr').each(function (index, ele) {
                let item =that.buildItem();
                (<any>item).title= $(ele).find('td').eq(0).find('a').text();
                (<any>item).url='http://www.bd.gov.cn/'+$(ele).find('td').eq(0).find('a').attr('href');
                item.source=that.name;
                let uri='http://www.bd.gov.cn/'+$(ele).find('td').eq(0).find('a').attr('href');
                (<any>item)['采购时间']=$(ele).find('td').eq(1).find('a').text().substring(1,10);
                result.push(item);
                result=that.getExtraItem(uri,item,result);
            });
        } catch (error) {
            console.error(error);
        }
        return result;
  }
  getExtraItem(url :string ,item :Item,result :Item[]) :Item[]{
    let content =this.spider.getSync(url);
    let $ =this.cheerio.load(content);
    (<any>item).content=$('.sj_nrbr').html();
    result.push(item);
    return result;
  }
  getPageCount ():number{
      return 20;
  }
}
module.exports=new bdcg();